﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Portal.Constants;
using NetDon.Utilities;
using NetDon.WebControls;


namespace NetDon.OA.Portal.Shunting
{

    public partial class IssuingOrderInfoEdit : NavigationPageBase
    {

        protected string Mode
        {
            get { return Parameters.GetString("Mode"); }
        }
        protected override IList<Guid> Ids
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.fieldIds.Value))
                    return new List<Guid>();
                return this.fieldIds.Value.Split(new char[] { ',' }).Select(id => id.ToGuid()).ToList();

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                if (Mode.ToLower() == "add")
                {
                    LoadInitialView();
                    btnReLoadIds.Enabled = false;
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (IssuringOrderEditUC.SaveOrder())
            {
                CloseWindow(true);
            }

        }

        private void LoadInitialView()
        {
            CurrentIndex = -1;
            this.IssuringOrderEditUC.LoadUserControl(Ids.Count > 0 ? Ids[++CurrentIndex] : Guid.Empty); 
            SetButtonState(Ids.Count <= 0, true, false, Ids.Count == 1, this.btnSave ,btnCancel,btnPrevious,btnNext,btnSaveAndPrevious,btnSaveAndNext,panelBasic);
 
        }

        void SetButtonState(bool isAdd, bool isFirst, bool isLast, bool isSingleEdit)
        {
            SetButtonState(isAdd, isFirst, isLast, isSingleEdit, this.btnSave, btnCancel, btnPrevious, btnNext, btnSaveAndPrevious, btnSaveAndNext, panelBasic);

        }


        protected override bool IsEditable()
        {
            return this.IssuringOrderEditUC.IsEditable();
        }
      
        protected void btnReLoadIds_Click(object sender, EventArgs e)
        {
            LoadInitialView();
            btnReLoadIds.Enabled = false;
        }



        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (IssuringOrderEditUC.SaveOrder())
            {
                IssuringOrderEditUC.LoadNavigationView(Guid.Empty);


            }
        }

        protected void btnSaveAndNext_Click(object sender, EventArgs e)
        {
            if (IssuringOrderEditUC.SaveOrder())
            {

                btnNext_Click(sender, e);

            }
        }
        protected void btnSaveAndPrevious_Click(object sender, EventArgs e)
        {
            if (IssuringOrderEditUC.SaveOrder())
            {

                btnPrevious_Click(sender, e);

            }
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {

            if (HasNextView())
            {
                IssuringOrderEditUC.LoadNavigationView(Ids[++CurrentIndex]);
            }

            SetButtonState(false, IsFirstView, IsLastView, false);



        }
        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (HasPreviousView())
            {
                IssuringOrderEditUC.LoadNavigationView(Ids[--CurrentIndex]);

            }
            SetButtonState(false, IsFirstView, IsLastView, false);
        }



    }
}